
clear
use ${data}preference_data.dta

* Exclude school outside options which aren't actually teachers
drop if outside_sch==1


* ------------------------------------------------------------------- *
* Estimate Teacher Preferences Three Ways *
* 1. Assume outside option is maximally desirable
* 2. Assume outside option is minimally desirable
* 3. Randomly draw rank of outside option from set of possible ranks 
* ------------------------------------------------------------------- *

eststo clear
cmset teacher_id, noalternative



eststo mid: cmrologit _rank_oo_mid Charter Traditional Alternative latitude_std longitude_std latlon_std enroll_std      majority_black   majority_hispanic no_majority majority_black_black majority_black_hisp   , cluster(teacher_id) 
gen s=e(sample)
unique teacher_id if s==1 & intDay==firstDay
estadd scalar nt= r(unique): mid
unique teacher_id if s==1
estadd scalar ntd = r(unique): mid
drop s


predict uhat_teacher_oo_mid0, xb
 label var uhat_teacher_oo_mid0 "Teachers Mean Utility from Matching with School, Mid Outside Option"

 
eststo low: cmrologit _rank_oo_low Charter Traditional Alternative latitude_std longitude_std latlon_std enroll_std      majority_black   majority_hispanic no_majority majority_black_black majority_black_hisp  , cluster(teacher_id) 
gen s=e(sample)
unique teacher_id if s==1 & intDay==firstDay
estadd scalar nt= r(unique): low
unique teacher_id if s==1
estadd scalar ntd = r(unique): low
drop s




predict uhat_teacher_oo_low0, xb
 label var uhat_teacher_oo_low0 "Teachers Mean Utility from Matching with School, Lower Bound Outside Option" 

 

eststo high: cmrologit _rank_oo_high Charter Traditional Alternative latitude_std longitude_std latlon_std enroll_std      majority_black   majority_hispanic no_majority majority_black_black majority_black_hisp  , cluster(teacher_id) 
gen s=e(sample)
unique teacher_id if s==1 & intDay==firstDay
estadd scalar nt= r(unique): high
unique teacher_id if s==1
estadd scalar ntd = r(unique): high
drop s



predict uhat_teacher_oo_high0, xb
 label var uhat_teacher_oo_high0 "Teachers Mean Utility from Matching with School, Upper Bound Outside Option"
 
 
 

estout mid using ${results}table1_teacherpref.txt, replace ///
	cells(b(star fmt(3) nostar)  se(par fmt(3))) ///
	collabels(,none) stat(nt ntd N) 
	
estout mid low high using ${results}tableA6.txt, replace ///
	cells(b(star fmt(3) nostar)  se(par fmt(3))) ///
	collabels(,none) stat(nt ntd N) 
	
	

* Bootstrap preference draws	
quietly {
forv i=1/$bootstraps {
	

preserve	
bsample, cluster(teacher_id)

cmrologit _rank_oo_low enroll_std Charter  Traditional Alternative  no_majority   majority_black   majority_hispanic majority_black_black  majority_black_hisp latitude_std longitude_std latlon_std , cluster(teacher_id) 


tempfile working
save `working', replace

restore 
predict uhat_teacher_oo_low`i', xb

preserve
use `working', clear

cmrologit _rank_oo_high enroll_std Charter  Traditional Alternative  no_majority   majority_black   majority_hispanic majority_black_black  majority_black_hisp latitude_std longitude_std latlon_std, cluster(teacher_id)  



restore 
predict uhat_teacher_oo_high`i', xb

preserve
use `working', clear

cmrologit _rank_oo_mid enroll_std Charter  Traditional Alternative  no_majority   majority_black   majority_hispanic majority_black_black  majority_black_hisp latitude_std longitude_std latlon_std  , cluster(teacher_id) 




restore 
predict uhat_teacher_oo_mid`i', xb



}	
}

	
* Restrict to actual teachers (no outside options)
keep if outside_cm==0
keep teacher_id schoolcert_id uhat_teacher* 
save ${data}Simulation/teacher_uhats.dta, replace



exit
